#!/usr/bin/env python3.12
# -*- coding: utf-8 -*-

import os
import time
import random
import json
import pathlib

aishuati_dir = pathlib.Path(__file__).parent.parent.parent.absolute() / "aishuati"
curpath = pathlib.Path(__file__).parent.absolute()

if __name__ == "__main__":
    # *********************生成论述题数据**********************
    n = 6
    with open(curpath / "lst.txt", "r", encoding="utf-8") as f:
        lsts = f.read().split("\n\n")

    for i in range(0, int(12 / n)):
        questions = []
        title = f"论述题_{i*n+1:03d}-{i*n+n:03d}"
        for j in range(i * n, (i + 1) * n):
            rows = lsts[j].split("\n")
            question = rows[0].strip()
            questions.append(
                {
                    "id": time.strftime("%Y%m%d%H%M", time.localtime())
                    + str(random.randint(0, 1000000)),
                    "title": question,
                    "option": "",
                    "answer": "</br></br>".join(rows[1:]),
                    "analysis": "",
                }
            )
        with open(aishuati_dir / f"json/{title}.json", "w", encoding="utf-8") as f:
            json.dump(questions, f, ensure_ascii=False, indent=4)
    questions = []
    for lst in lsts:
        rows = lst.split("\n")
        question = rows[0].strip().split(".")[1]
        questions.append(
            {
                "id": time.strftime("%Y%m%d%H%M", time.localtime())
                + str(random.randint(0, 1000000)),
                "title": question,
                "option": "",
                "answer": "</br></br>".join(rows[1:]),
                "analysis": "",
            }
        )
    with open(aishuati_dir / "json/论述题(全部).json", "w", encoding="utf-8") as f:
        json.dump(questions, f, ensure_ascii=False, indent=4)

    # *********************生成简答题数据**********************
    n = 6
    with open(curpath / "jdt.txt", "r", encoding="utf-8") as f:
        jdts = f.read().split("\n\n")

    for i in range(0, int(18 / n)):
        questions = []
        title = f"简答题_{i*n+1:03d}-{i*n+n:03d}"
        for j in range(i * n, (i + 1) * n):
            rows = jdts[j].split("\n")
            question = rows[0].strip()
            questions.append(
                {
                    "id": time.strftime("%Y%m%d%H%M", time.localtime())
                    + str(random.randint(0, 1000000)),
                    "title": question,
                    "option": "",
                    "answer": "</br></br>".join(rows[1:]),
                    "analysis": "",
                }
            )
        with open(aishuati_dir / f"json/{title}.json", "w", encoding="utf-8") as f:
            json.dump(questions, f, ensure_ascii=False, indent=4)
    questions = []
    for jdt in jdts:
        rows = jdt.split("\n")
        question = rows[0].strip().split(".")[1]
        questions.append(
            {
                "id": time.strftime("%Y%m%d%H%M", time.localtime())
                + str(random.randint(0, 1000000)),
                "title": question,
                "option": "",
                "answer": "</br></br>".join(rows[1:]),
                "analysis": "",
            }
        )
    with open(aishuati_dir / "json/简答题(全部).json", "w", encoding="utf-8") as f:
        json.dump(questions, f, ensure_ascii=False, indent=4)

    # *********************生成选择题数据**********************
    n = 30
    mapping = {"A": 1, "B": 2, "C": 3, "D": 4}
    with open(curpath / "xzt.txt", "r", encoding="utf-8") as f:
        xzts = f.read().split("\n\n")
    with open(curpath / "xztda.txt", "r", encoding="utf-8") as f:
        xztda = f.readlines()
    for i in range(0, int(300 / n)):
        questions = []
        title = f"选择题_{i*n+1:03d}-{i*n+n:03d}"

        for j in range(i * n, (i + 1) * n):
            rows = xzts[j].split("\n")
            answer = xztda[j].strip().split(".")[1]
            question = rows[0].strip()
            questions.append(
                {
                    "id": time.strftime("%Y%m%d%H%M", time.localtime())
                    + str(random.randint(0, 1000000)),
                    "title": question,
                    "option": [
                        ".".join(row.split(".")[1:]) for row in rows[1:] if row != ""
                    ],
                    "answer": answer,
                    "analysis": "",
                }
            )
        with open(aishuati_dir / f"json/{title}.json", "w", encoding="utf-8") as f:
            json.dump(questions, f, ensure_ascii=False, indent=4)
    questions = []
    for j, xzt in enumerate(xzts):
        answer = xztda[j].strip().split(".")[1]
        rows = xzts[j].split("\n")
        question = "".join(rows[0].strip().split(".")[1:])
        questions.append(
            {
                "id": time.strftime("%Y%m%d%H%M", time.localtime())
                + str(random.randint(0, 1000000)),
                "title": question,
                "option": [
                    ".".join(row.split(".")[1:]) for row in rows[1:] if row != ""
                ],
                "answer": answer,
                "analysis": "",
            }
        )
    with open(aishuati_dir / "json/选择题(全部).json", "w", encoding="utf-8") as f:
        json.dump(questions, f, ensure_ascii=False, indent=4)

    # *********************生成判断题数据**********************
    n = 50
    mapping = {"对": "A", "错": "B"}
    with open(curpath / "pdt.txt", "r", encoding="utf-8") as f:
        pdts = f.readlines()
    with open(curpath / "pdtda.txt", "r", encoding="utf-8") as f:
        pdtda = f.readlines()
    for i in range(0, int(150 / n)):
        questions = []
        title = f"判断题_{i*n+1:03d}-{i*n+n:03d}"
        for j in range(i * n, (i + 1) * n):
            answer = pdtda[j].strip().split(".")[1]
            question = pdts[j].strip()
            questions.append(
                {
                    "id": time.strftime("%Y%m%d%H%M", time.localtime())
                    + str(random.randint(0, 1000000)),
                    "title": question,
                    "option": ["对", "错"],
                    "answer": mapping[answer],
                    "analysis": "",
                }
            )

        with open(aishuati_dir / f"json/{title}.json", "w", encoding="utf-8") as f:
            json.dump(questions, f, ensure_ascii=False, indent=4)

    questions = []
    for j, pdt in enumerate(pdts):
        answer = pdtda[j].strip().split(".")[1]
        question = "".join(pdts[j].strip().split(".")[1:])
        questions.append(
            {
                "id": time.strftime("%Y%m%d%H%M", time.localtime())
                + str(random.randint(0, 1000000)),
                "title": question,
                "option": ["对", "错"],
                "answer": mapping[answer],
                "analysis": "",
            }
        )
    with open(aishuati_dir / "json/判断题(全部).json", "w", encoding="utf-8") as f:
        json.dump(questions, f, ensure_ascii=False, indent=4)

    # f = open(
    #     "/dev/shm/" + time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".json",
    #     "w",
    #     encoding="utf-8",
    # )
    # f.write(json.dumps(questions, ensure_ascii=False, indent=4))
    # f.close()

    # questionsData[f"chapter{i+1}"] = {"title": title, "questions": questions}
    # print("var questionsData = ", questionsData)
    # with open("/dev/shm/pdt.js", "w", encoding="utf-8") as f:
    #     f.write("var questionsData = " + str(questionsData))

    # # *********************生成选择题数据**********************
    # mapping = {"A": 1, "B": 2, "C": 3, "D": 4}
    # with open(curpath + "/xzt.txt", "r", encoding="utf-8") as f:
    #     xzts = f.read().split("\n\n")
    # with open(curpath + "/xztda.txt", "r", encoding="utf-8") as f:
    #     xztda = f.readlines()
    # questionsData = {}
    # for i in range(0, int(300 / n)):
    #     questions = []
    #     id = 1
    #     title = f"练习{i+1}"
    #     for j in range(i * n, (i + 1) * n):
    #         row = xzts[j].split("\n")
    #         daan = xztda[j].strip().split(".")[1]
    #         question = row[0].strip().split(".")[1]
    #         questions.append(
    #             {
    #                 "id": id,
    #                 "question": question,
    #                 "options": [row[1], row[2], row[3], row[4]],
    #                 "answer": row[mapping[daan]],
    #             }
    #         )
    #         id += 1
    #     questionsData[f"chapter{i+1}"] = {"title": title, "questions": questions}
    # print("var questionsData = ", questionsData)
    # with open("/dev/shm/xzt.js", "w", encoding="utf-8") as f:
    #     f.write("var questionsData = " + str(questionsData))
